{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "782c6584",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7951c016",
   "metadata": {},
   "source": [
    "文件保存：\n",
    "使用np.savetxt来实现：  \n",
    "np.savetxt(frame,array,fmt,delimiter,newline,header,footer,comments,encoding)    \n",
    "frame:文件名或文件句柄   \n",
    "array:要保存到文件的数据   \n",
    "fmt:保存的格式  \n",
    "delimiter:分割字符串，默认是空格   \n",
    "newline:字符串或字符分隔线   \n",
    "header:将在文件开头写入的字符串   \n",
    "footer：将在文件末尾写入的字符串   \n",
    "comments：将附加到header和footer字符串的字符串   \n",
    "encoding：文件编码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "229dff02",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[16, 34],\n",
       "       [96, 27],\n",
       "       [16, 59],\n",
       "       [72, 54],\n",
       "       [51,  9],\n",
       "       [55, 18],\n",
       "       [50, 19],\n",
       "       [55, 14],\n",
       "       [72, 86],\n",
       "       [67, 61],\n",
       "       [73, 14],\n",
       "       [69,  9],\n",
       "       [ 7, 39],\n",
       "       [32, 88],\n",
       "       [18, 83],\n",
       "       [71, 46],\n",
       "       [95, 66],\n",
       "       [99, 24],\n",
       "       [78, 27],\n",
       "       [53, 30]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scores = np.random.randint(0,100,size=(20,2))\n",
    "scores"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b3a7ce3c",
   "metadata": {},
   "outputs": [],
   "source": [
    "np.savetxt(\"score.csv\",scores,delimiter=',',header=\"英语,数学\",comments=\"\",fmt=\"%d\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1286266d",
   "metadata": {},
   "source": [
    "读取文件：\n",
    "使用np.loadtxt来实现，相关函数描述如下:\n",
    "np.loadtxt(frame, dtype=np.float, delimiter=None, unpack=False)\n",
    "* frame：文件、字符串或产生器，可以是.gz或.bz2的压缩文件。\n",
    "* dtype：数据类型，可选。\n",
    "* delimiter：分割字符串，默认是任何空格。\n",
    "* skiprows：跳过前面x行。\n",
    "* usecols：读取指定的列，用元组组合。\n",
    "* unpack：如果True，读取出来的数组是转置后的。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7c15bc8e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[16, 34],\n",
       "       [96, 27],\n",
       "       [16, 59],\n",
       "       [72, 54],\n",
       "       [51,  9],\n",
       "       [55, 18],\n",
       "       [50, 19],\n",
       "       [55, 14],\n",
       "       [72, 86],\n",
       "       [67, 61],\n",
       "       [73, 14],\n",
       "       [69,  9],\n",
       "       [ 7, 39],\n",
       "       [32, 88],\n",
       "       [18, 83],\n",
       "       [71, 46],\n",
       "       [95, 66],\n",
       "       [99, 24],\n",
       "       [78, 27],\n",
       "       [53, 30]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = np.loadtxt(\"score.csv\",dtype=int,delimiter=',',skiprows=1)\n",
    "b"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6c822bd8",
   "metadata": {},
   "source": [
    "numpy独有的存储解决方案：\n",
    "存储：np.save(frame,array)或np.savez(frame,array),前者函数的扩展名是`.npy`，后者的扩展名是`.npz`，后者是经过压缩的   \n",
    "加载：np.load(fname)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "bcc4ae09",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[8 5 9]\n",
      " [6 0 9]]\n"
     ]
    }
   ],
   "source": [
    "c = np.random.randint(0,10,size=(2,3))\n",
    "print(c)\n",
    "np.save(\"c\",c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "66bb583e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[8, 5, 9],\n",
       "       [6, 0, 9]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c1 = np.load(\"c.npy\")\n",
    "c1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "54349eb4",
   "metadata": {},
   "outputs": [],
   "source": [
    "d = np.random.randint(0,10,size=(2,3,4))\n",
    "# np.savetxt(\"d.csv\",d)\n",
    "np.save(\"d\",d)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3399286",
   "metadata": {},
   "source": [
    "# 总结：\n",
    "1.np.savetxt和np.loadtxt一般用来操作csv文件，它可以设置header，但是不能存储三维以上的数组。  \n",
    "2.np.save和np.load一般用来存储非文本类型的文件，它不可以设置header，但是可以存储三维以上的数组。   \n",
    "3.如果想专门的操作CSV文件，还有csv模块。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
